<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="qq" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="showDialog = true"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" default-expand-all>
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="add(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 子部门或编辑部门的弹框 -->
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        title="新增子部门或编辑部门"
        :visible.sync="showDialog"
        width="50%"
        @closed="hClosed"
      >
        <addOrEdit
          :id="curId"
          ref="deptRef"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="showDialog = false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'

import addOrEdit from './deptDialog.vue'

export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [], //  存储部门数据
      showDialog: false, // 控制弹框的显示隐藏
      curId: '',
      isEdit: false,
      originList: '' // 保存传给子组件的部门列表
    }
  },

  // 调用logDepartments发送请求
  created() {
    this.logDepartments()
  },

  methods: {
    // 定义logDepartments函数发送请求
    async logDepartments() {
      try {
        const res = await getDepartments()
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.originList = res.data.depts.map(item => {
          const { id, code, name, pid } = item
          return { id, code, name, pid }
        })
        this.$message.success(res.message)
        res.data.depts.shift() // 删除第一条数据
        // 调用tranListToTreeData完成树形转换
        this.list = tranListToTreeData(res.data.depts)
      } catch (error) {
        this.$message.error('获取数据失败')
      }
    },
    add(id) {
      // console.log(id)
      this.curId = id
      this.isEdit = false
      this.showDialog = true
    },
    hSuccess() {
      this.logDepartments()
      this.showDialog = false
    },
    hEdit(id) {
      this.curId = id
      this.isEdit = true
      // 解决数据不更新的方法2
      // this.$nextTick(() => {
      //   this.$refs.deptRef.loadDepartmentsDetail()
      // })
      this.showDialog = true
    },
    // 用户点击删除
    // 删除部门
    hDel(id) {
      this.$confirm('您确定要删除吗，是否继续？', '提示', {
        confirmButtonText: '确定',
        concelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 确定删除
        const res = await delDepartments(id)
        console.log(res)
        if (res.code === 10000) {
          this.$message.success(res.message)
          this.logDepartments()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hClosed() {
      // 重置表单的检验规则
      console.log('关闭弹框')
      this.$refs.deptRef.resetForm()
    }
  }
}
</script>
